package ui;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import config.FrameConfig;
import config.GameConfig;
import dto.GameDto;

/**
 * 绘制窗口
 * @author ck
 * @param g 画笔
 * 
 */
public abstract class Layer {
	
	/**
	 * 内边距
	 */
	protected static final int PADDING; 
	
	/**
	 * 边框宽度
	 */
	protected static final int BORDER;
	
	private static final int STR_NUM_WIDTH = 11; 
	
	static{
		//获得界面配置
		FrameConfig fcfg = GameConfig.getFrameConfig();
		PADDING = fcfg.getPadding();
		BORDER = fcfg.getBorder();
	}
	
	/**
	 *窗体边框切片宽度 
	 */
	private static int WINDOW_W = Img.WINDOW.getWidth(null);
	
	/**
	 *窗体边框切片 高度
	 */
	private static int WINDOW_H = Img.WINDOW.getHeight(null);
		
	/**
	 * 数字切片的宽度
	 */
	protected static final int IMG_NUMBER_W = Img.NUMBER.getWidth(null) / 10;
	
	/**
	 * 数字切片的高度
	 */
	private static final int IMG_NUMBER_H = Img.NUMBER.getHeight(null);
	
	/**
	 * 矩形值槽图片高度
	 */
	protected static final int IMG_RECT_H = Img.RECT.getHeight(null);
	
	/**
	 * 矩形值槽图片宽度
	 */
	private static final int IMG_RECT_W = Img.RECT.getWidth(null);
	
	/**
	 * 矩形值槽宽度
	 */
	private final int rectW;
	
	/**
	 * 值槽字体颜色
	 */
	protected static final Font DEF_FONT = new Font("黑体", Font.BOLD, 20);
	
	/**
	 * 窗口左上角x坐标
	 */
	protected final int x;
	
	/**
	 * 窗口左上角y坐标
	 */
	protected final int y;
	
	/**
	 * 窗口宽度
	 */
	protected final int w;
	
	/**
	 * 窗口高度
	 */
	protected final int h;
	
	/**
	 * 游戏数据
	 */
	protected GameDto dto = null;
	
	/**
	 * 设置窗体显示位置
	 * 
	 * @param x
	 * @param y
	 * @param w
	 * @param h
	 */
	protected Layer(int x, int y, int w, int h){
		
		this.x = x;
		this.y = y;
		this.w = w;
		this.h = h;
		rectW = this.w - (PADDING << 1);
	}
	
	/**
	 * 绘制窗体
	 * @param g
	 */
	protected void createWindow(Graphics g){
		
		//左上角
		g.drawImage(Img.WINDOW, x, y, x + BORDER, y + BORDER, 0, 0, BORDER, BORDER, null);
		//右上角
		g.drawImage(Img.WINDOW, x + w - BORDER, y, x + w, y + BORDER, WINDOW_W - BORDER, 0, WINDOW_W, BORDER, null);
		//左下角
		g.drawImage(Img.WINDOW, x, y + h - BORDER, x + BORDER, y + h, 0, WINDOW_H - BORDER, BORDER, WINDOW_H, null);
		//右下角
		g.drawImage(Img.WINDOW, x + w - BORDER, y + h - BORDER, x + w, y + h, WINDOW_W - BORDER, WINDOW_H - BORDER, WINDOW_W, WINDOW_H, null);		
		//左边
		g.drawImage(Img.WINDOW, x, y + BORDER, x + BORDER, y + h - BORDER, 0, BORDER, BORDER, WINDOW_H - BORDER, null);		
		//右边
		g.drawImage(Img.WINDOW, x + w - BORDER, y + BORDER, x + w, y + h - BORDER, WINDOW_W - BORDER, BORDER, WINDOW_W, WINDOW_H - BORDER, null);		
		//上边
		g.drawImage(Img.WINDOW, x + BORDER, y, x + w - BORDER, y + BORDER, BORDER, 0, WINDOW_W - BORDER, BORDER, null);		
		//下边
		g.drawImage(Img.WINDOW, x + BORDER, y + h - BORDER, x + w - BORDER, y + h, BORDER, WINDOW_H - BORDER, WINDOW_W - BORDER, WINDOW_H, null);		
		//中部
		g.drawImage(Img.WINDOW, x + BORDER, y + BORDER, x + w - BORDER, y + h - BORDER, BORDER, BORDER, WINDOW_W - BORDER, WINDOW_H - BORDER, null);		

	}
	
	/**
	 * 设置游戏数据对象
	 */
	public void setDto(GameDto dto) {
		this.dto = dto;
	}
	
	/**
	 * 刷新游戏具体内容
	 * @param g
	 * @author Ck
	 */
	abstract public void paint(Graphics g);
	
	/**
	 * 显示数字图片
	 * 
	 * @param x 左上角x坐标
	 * @param y 左上角y坐标
	 * @param num 要显示的数字
	 * @param maxBit 数字位数
	 * @param g 画笔对象
	 */
	protected void drawNumLeftPad(int x, int y, int num, int maxBit,Graphics g){
		//把要打印的数字转成字符串
		String strNum = Integer.toString(num);
		//循环绘制数字右对齐
		for (int i = 0; i < maxBit; i++) {
			//判断是否满足绘制条件
			if (maxBit - i <= strNum.length()) {
				//获得数字在字符串中的下标
				int idx = i - maxBit + strNum.length();
				//把数字number中的每一位取出
				int bit = strNum.charAt(idx) - '0';
				//绘制数字
				g.drawImage(Img.NUMBER, 
						this.x + x + IMG_NUMBER_W * i, this.y + y, 
						this.x + x + IMG_NUMBER_W * (i + 1), this.y + y + IMG_NUMBER_H,
						bit * IMG_NUMBER_W, 0, 
						(bit + 1) * IMG_NUMBER_W, 
						IMG_NUMBER_H, null);
			}
		}
	}
	
	/**
	 * 显示数字字符串
	 */
	protected void drawStrNumLeftPad(int x, int y, String strNum, int maxBit,Graphics g){
		//循环绘制数字右对齐
		for (int i = 0; i < maxBit; i++) {
			//判断是否满足绘制条件
			if (maxBit - i <= strNum.length()) {
				//获得数字在字符串中的下标
				int idx = i - maxBit + strNum.length();
				//把数字number中的每一位取出
				int num = strNum.charAt(idx) - '0';
				//把取出的数字转成String类型
				String bit = Integer.toString(num); 
				//绘制数字
				g.drawString(bit, x + (i + 1) * STR_NUM_WIDTH, y);
			}
		}
	}
	
	/**
	 * 绘制值槽
	 */
	protected void drawRect(int y, String title, String number, double percent, Graphics g){
		//各种值初始化
		int rect_x = this.x + PADDING;
		int rect_y = this.y + y;
		//绘制背景
		g.setColor(Color.BLACK);
		g.fillRect(rect_x, rect_y, rectW, IMG_RECT_H + 4);
		g.setColor(Color.WHITE);
		g.fillRect(rect_x + 1, rect_y + 1, rectW - 2, IMG_RECT_H + 2);
		g.setColor(Color.BLACK);
		g.fillRect(rect_x + 2, rect_y + 2, rectW - 4, IMG_RECT_H);
		//求出宽度
		int w = (int)(percent * (rectW - 4));
		//求出颜色
		int subIdx = (int)(percent * IMG_RECT_W) - 1;
		//绘制值槽
		g.drawImage(Img.RECT,
				rect_x + 2, rect_y + 2,
				rect_x + 2 + w, rect_y + 2 + IMG_RECT_H,
				subIdx, 0, subIdx + 1, IMG_RECT_H,
				null);
		g.setColor(Color.WHITE);
		g.setFont(DEF_FONT);
		g.drawString(title, rect_x + 4, rect_y + 23);
		if (number != null) {
			this.drawStrNumLeftPad(rect_x + 228, rect_y + 23, number, 5, g);
		}
	}
	
	/**
	 * 绘图居中
	 */
	protected void drawImageAtCenter(Image img, Graphics g){
		int imgW = img.getWidth(null);
		int imgH = img.getHeight(null);
		int imgX = this.x + (this.w - imgW >> 1);
		int imgY = this.y + (this.h - imgH >> 1);
		g.drawImage(img, imgX, imgY, null);
	}
}
